package test.org.spring.boot.example.dds.service;

import javax.annotation.Resource;

import org.spring.boot.example.dds.DataSourceName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("userService")
@DataSourceName("dataSource1")
public class UserServiceImpl implements UserService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Resource
	private UserService userServiceBackup;

	@Autowired
	private UserLogService userLogService;

	@Override
	public void updateUserName(Integer id, String username) {

	}

	@Override
	public String selectUsernameById(Integer id) {
		return jdbcTemplate.queryForObject("select username from user where id=?", String.class, id);
	}

	@Override
	@Transactional
	public void addUser(Integer id, String username) {
		jdbcTemplate.update("insert into user(id,username) values(?,?)", id, username);
		userServiceBackup.addUser(id, username);
		userLogService.addUserLog(id, UserLogService.ADD);
	}

	@Transactional
	public void clear() {
		jdbcTemplate.update("delete from user");
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public void setUserServiceBackup(UserService userServiceBackup) {
		this.userServiceBackup = userServiceBackup;
	}

}
